{% extends "base.html" %}

{% block title %}复盘系统 - 机构调研报告{% endblock %}

{% block page_title %}机构调研报告{% endblock %}
{% block page_subtitle %}实时更新的上市公司机构调研信息{% endblock %}

{% block extra_css %}
<style>
    /* 自适应列宽 */
    th:nth-child(1), td:nth-child(1) { width: 10%; min-width: 80px; } /* 证券代码 */
    th:nth-child(2), td:nth-child(2) { width: 20%; min-width: 120px; } /* 公司名称 */
    th:nth-child(3), td:nth-child(3) { width: 50%; min-width: 200px; } /* 调研报告名称 */
    th:nth-child(4), td:nth-child(4) { width: 20%; min-width: 140px; } /* 日期 */

    /* 移动端表格列宽调整 */
    @media (max-width: 768px) {
        th:nth-child(1), td:nth-child(1) { width: 15%; min-width: 70px; }
        th:nth-child(2), td:nth-child(2) { width: 25%; min-width: 100px; }
        th:nth-child(3), td:nth-child(3) { width: 40%; min-width: 150px; }
        th:nth-child(4), td:nth-child(4) { width: 20%; min-width: 120px; }
    }

    @media (max-width: 480px) {
        th:nth-child(1), td:nth-child(1) { width: 18%; min-width: 60px; }
        th:nth-child(2), td:nth-child(2) { width: 30%; min-width: 80px; }
        th:nth-child(3), td:nth-child(3) { width: 35%; min-width: 120px; }
        th:nth-child(4), td:nth-child(4) { width: 17%; min-width: 100px; }
    }

    .tabs { padding: 1rem 1.5rem 0; }
    .tab-link { margin-right:12px; padding:6px 12px; border-radius:16px; text-decoration:none; }
    .tab-active { background:#8b7355; color:#fff; }
    .tab-inactive { background:#eee; color:#333; }
    .pdf-tag { margin-right:8px; color:#d9534f; font-weight:600; }
    .doc-tag { margin-right:8px; color:#0275d8; font-weight:600; }

    .page-jump { margin-top:10px; text-align:center; }
    .jump-input { width:60px; padding:4px; border-radius:4px; border:1px solid #ccc; text-align:center; }
    .jump-button { padding:4px 8px; background:#8b7355; color:#fff; border:none; border-radius:4px; cursor:pointer; margin-left:5px; }
</style>
{% endblock %}

{% block content %}
<div class="tabs">
    <a href="{{ request.url.replace_query_params(market='sz', page=1) }}" class="tab-link {% if market!='sh' %}tab-active{% else %}tab-inactive{% endif %}">深市</a>
    <a href="{{ request.url.replace_query_params(market='sh', page=1) }}" class="tab-link {% if market=='sh' %}tab-active{% else %}tab-inactive{% endif %}">沪市</a>
</div>
<div class="table-container">
    <table>
        <thead>
            <tr>
                <th scope="col">证券代码</th>
                <th scope="col">公司名称</th>
                <th scope="col">调研报告名称</th>
                <th scope="col">日期</th>
            </tr>
        </thead>
        <tbody>
            {% for item in data %}
            <tr>
                <td><a href="/c/{{ item.secCode }}" target="_blank">{{ item.secCode }}</a></td>
                <td>{{ item.secName }}</td>
                <td>
                    {% if item.fileType %}
                        {% if item.fileType == 'pdf' %}
                            <span class="pdf-tag">[PDF]</span>
                        {% elif item.fileType in ['doc','docx'] %}
                            <span class="doc-tag">[DOC]</span>
                        {% endif %}
                    {% endif %}
                    <a href="{{ item.adjunctUrl }}" target="_blank">{{ item.announcementTitle }}</a>
                </td>
                <td>{{ item.announcementTime }}</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</div>
{% endblock %}

{% block pagination %}
{% if pages and page %}
<div class="pagination">
    <h3>共 {{pages}} 页</h3>
    <div class="pagination-links">
        {% if page > 1 %}
        <a href="{{ request.url.replace_query_params(page=page-1, market=market or 'sz') }}">← 上一页</a>
        {% endif %}
        <span class="current-page">当前为第 {{page}} 页</span>
        {% if page < pages %}
        <a href="{{ request.url.replace_query_params(page=page+1, market=market or 'sz') }}">下一页 →</a>
        {% endif %}
    </div>
    <div class="page-jump">
        <form method="get" action="{{ request.path_url }}">
            {% for key, value in request.query_params.items() if key != 'page' %}
            <input type="hidden" name="{{key}}" value="{{value}}">
            {% endfor %}
            <input type="number" name="page" min="1" max="{{pages}}" value="{{page}}" class="jump-input">
            <button type="submit" class="jump-button">跳转</button>
        </form>
    </div>
</div>
{% endif %}
{% endblock %}

{% block extra_js %}
<script>
    // 时间戳转换为指定格式的日期
    function formatDate(timestamp) {
        const date = new Date(timestamp);
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        const hours = String(date.getHours()).padStart(2, '0');
        const minutes = String(date.getMinutes()).padStart(2, '0');
        const seconds = String(date.getSeconds()).padStart(2, '0');

        return `${year}-${month}-${day}  ${hours}:${minutes}:${seconds}`;
    }

    // 限制报告名称的长度并更新日期格式
    function updateTable() {
        const rows = document.querySelectorAll('tbody tr');
        rows.forEach(row => {
            const titleCell = row.cells[2];
            const dateCell = row.cells[3];

            // 限制报告名称的长度（仅修改链接文本，不替换整列，避免移除 <a> 标签）
            const link = titleCell.querySelector('a');
            if (link) {
                const title = link.textContent || '';
                if (title.length > 50) {
                    link.textContent = title.slice(0, 50) + '...';
                }
            }

            // 更新日期格式
            const raw = dateCell.textContent.trim();
            const timestamp = parseInt(raw, 10);
            if (!isNaN(timestamp) && String(timestamp).length >= 10) {
                const ts = timestamp > 1e12 ? timestamp : timestamp * 1000;
                dateCell.textContent = formatDate(ts);
            }
        });
    }

    // 页面加载完成后执行updateTable函数
    window.addEventListener('DOMContentLoaded', updateTable);
</script>
{% endblock %}